package com.wsjzzcbq.exception;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * GlobalExceptionHandler
 *
 * @author wsjz
 * @date 2023/10/08
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Object exceptionHandler(Exception e){
        log.error("异常:", e);
        return "网络异常";
    }

//    @ExceptionHandler
//    public Object exceptionHandler(Exception e){
//        log.error("异常:", e);
//        if (e instanceof UndeclaredThrowableException) {
//            UndeclaredThrowableException ute = (UndeclaredThrowableException)e;
//            if (ute.getCause() instanceof DegradeException) {
//                log.error(ute.getCause().getMessage(), ute.getCause());
//                return "您已被限流";
//            }
//        }
//        return "网络异常";
//    }

    @ExceptionHandler(DegradeException.class)
    public Object exceptionHandler(DegradeException e){
        log.error("DegradeException异常:", e);
        return "您已被限流";
    }
}
